package com.app.common.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.app.common.util.Logger;
import com.app.common.util.Tools;

/**
 * Created by zhoulikai on 17/2/16.
 */

public abstract class BaseFragment extends Fragment {
    private final static String TAG = "BaseFragment";

    public abstract int initLayoutId();

    public abstract void initPage(View view);

    public abstract void parseParameter(Bundle bundle);

    /**
     * 实例化fragment
     *
     * @param className
     * @param bundle
     * @return
     */
    public static BaseFragment newInstance(String className, Bundle bundle) {
        BaseFragment baseFragment = null;
        try {

            Class cls = Class.forName(className);
            baseFragment = (BaseFragment) cls.newInstance();
            //fragment保存参数，传入一个Bundle对象
            baseFragment.setArguments(bundle);
            return baseFragment;

        } catch (Exception e) {
            Logger.d(TAG, ">>>初始化fragment实例失败，失败原因>>>" + e);

        }
        return baseFragment;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseParameter(this.getArguments());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(initLayoutId(), null);
        initPage(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
